<template>
  <div class="home-page">
    <div class="banner">
      <div class="container">
        <h1 class="logo-font">conduit</h1>
        <p>A place to share your knowledge.</p>
      </div>
    </div>

    <div class="container page">
      <div class="row">
        <div class="col-md-9">
          <div class="feed-toggle">
            <ul class="nav nav-pills outline-active">
              <li class="nav-item" v-if="user">
                <nuxt-link
                  class="nav-link"
                  :class="{active:tab === 'your_feed'}"
                  
                  :to="{ name: 'home', query:{ tab:'your_feed' }}"
                >Your Feed</nuxt-link>
              </li>
              <li class="nav-item">
                <nuxt-link
                  class="nav-link"
                  :class="{active:tab === 'global_feed'}"
                  :to="{ name: 'home', query:{ tab:'global_feed' }}"
                >Global Feed</nuxt-link>
              </li>
            </ul>
          </div>
          <!-- 文章列表 -->
          <article-list :articles="articles" />
          <!-- 分页列表 -->
          <page-list
            :totalPage="totalPage"
            :page="page"
            routerName="home"
            v-show="totalPage>limit"
          />
        </div>

        <div class="col-md-3">
          <div class="sidebar">
            <p>Popular Tags</p>

            <div class="tag-list">
              <nuxt-link
                :to="{
                name:'home',
                query:{
                  tag: item,
                  tab:$route.query.tab
                }
              }"
                class="tag-pill tag-default"
                v-for="item in tags"
                :key="item"
              >{{ item }}</nuxt-link>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import ArticleList from "@/components/article-list";
import PageList from "@/components/page-list";
import { getTags } from "@/api/tag";
import { getArticles, getFeed } from "@/api/article";
export default {
  name: "HomeIndex",
  components: {
    ArticleList,
    PageList,
  },
  async asyncData({ query, store }) {
    const { tab = "global_feed" } = query; //tab 区别feed
    const methodObj = {
      your_feed: getFeed,
      global_feed: getArticles,
    };
    const page = Number.parseInt(query.page || 1);
    const limit = 10;
    const [articleRes, tagRes] = await Promise.all([
      methodObj[tab]({
        limit,
        offset: (page - 1) * limit,
        tag: query.tag,
      }),
      getTags(),
    ]);
    const { articles, articlesCount } = articleRes.data;
    const { tags } = tagRes.data;
    articles.forEach((article) => (article.favoriteDisabled = false));
    return {
      articles,
      articlesCount,
      limit,
      page,
      tags,
      tab,
      user: store.state.user
    };
  },
  watchQuery: ["page", "tag", "tab"],
  computed: {
    totalPage() {
      return Math.ceil(this.articlesCount / this.limit);
    },
  },
};
</script>